﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FMALog;
using FMAPortalBase.Model;
using System.Web.Mvc;

namespace FMAPortalBase.Manager
{
    public class FileManager
    {
        private String path { get; set; }
        public FileManager(String path)
        {
            this.path = path;
        }
        public FileManager()
        {
            this.path = Const.ROOT_PATH;
        }
        public void Create(String data, String file)
        {
            if (File.Exists(this.path + file))
            {
                Exception exc = new Exception("Bu isimde bir dosya var");
                throw exc;
            }
            using (StreamWriter sw = new StreamWriter(this.path + "\\"+ file))
            {
                try
                {
                    //File.Create(this.path + file).Dispose();
                    sw.WriteLineAsync(data);
                }
                catch (Exception ex)
                {
                    ex.WriteLog();
                    throw ex;
                }
            }
        }
        public void Write(String data, String file)
        {
            using (StreamWriter sw = new StreamWriter(this.path + file))
            {
                try
                {
                    if (!File.Exists(this.path + file))
                        File.Create(this.path + file).Dispose();
                    sw.WriteLineAsync(data);
                }
                catch (Exception ex)
                {
                    ex.WriteLog();
                }
            }
        }
        public StringBuilder ReadAll(String file)
        {
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(this.path + file))
            {
                try
                {
                    sb.Append(sr.ReadToEnd());
                }
                catch (Exception ex)
                {
                    ex.WriteLog();
                }
            }
            return sb;
        }

        [AcceptVerbs("GET", "POST")]
        [HttpPost]
        public List<FileItem> GetAllFileItemList(String path)
        {
            path = Const.ROOT_PATH + path;
            List<FileItem> fileItemList = new List<FileItem>();
            String[] directoryList = GetAllDirectory(path);
            fileItemList.AddRange(directoryList.Select(x => new FileItem() { IsDirectory = true, Name = x.Replace(Const.ROOT_PATH, "") }).ToList());
            List<FileInfo> fileList = GetAllFiles(path);
            foreach (FileInfo info in fileList)
            {
                FileItem fileItem = new FileItem();
                try
                {
                    fileItem.PageInfo = info.OpenText().ReadToEnd().DeSerialize<Page>();
                }
                catch
                {
                    fileItem.PageInfo = null;
                }
                fileItem.IsDirectory = false;
                fileItem.Name = info.FullName.Replace(Const.ROOT_PATH,"");
                fileItemList.Add(fileItem);
            }
            //fileItemList.AddRange(GetAllFiles().Select(x => new FileItem() { IsDirectory = false, Name = x.Name }).ToList());
            return fileItemList;
        }

        public String[] GetAllDirectory(String path)
        {
            if (String.IsNullOrEmpty(path))
            {
                return Directory.GetDirectories(this.path);
            }
            else
                if (Directory.Exists(path))
                    return Directory.GetDirectories(path);
                else
                    return null;
        }

        public List<FileInfo> GetAllFiles(String path)
        {
            if (String.IsNullOrEmpty(path))
            {
                return Directory.GetFiles(this.path).Select(x => new FileInfo(x)).ToList<FileInfo>();
            }
            else
                if (Directory.Exists(path))
                    return Directory.GetFiles(path).Select(x => new FileInfo(x)).ToList<FileInfo>();
                else
                    return null;
        }

        public void delete(String obj,Boolean file=true)
        {
            String dest = Const.ROOT_PATH + Const.TRASH_PATH;
            if (file)
            {
                String _dest = dest +"\\"+  obj.Substring(0,obj.LastIndexOf('\\'));
                if (!Directory.Exists(_dest))
                {
                    Directory.CreateDirectory(_dest);
                }
                System.IO.File.Move(Const.ROOT_PATH + obj, dest + obj);
            }
            else
            {
                dest += obj;
                if (!Directory.Exists(dest))
                {
                    Directory.CreateDirectory(dest);
                }
                Directory.Move(Const.ROOT_PATH + obj, dest);
            }
        }
    }
}
